package com.open4cn.simulator.parser.datacolor.dc1000;

import com.open4cn.simulator.answer.LogInstructionAction;
import com.open4cn.simulator.answer.datacolor.GreenboardAction;
import com.open4cn.simulator.answer.datacolor.UV0Greenboard1000Answer;
import com.open4cn.simulator.answer.datacolor.UV100Greenboard1000Answer;
import com.open4cn.simulator.core.answer.AbstractAnswer;
import com.open4cn.simulator.core.answer.AnswerResponse;
import com.open4cn.simulator.core.ext.DataColorStatus;
import com.open4cn.simulator.core.parser.IInstructionParser;
import com.open4cn.simulator.core.parser.InstructionParserChain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.List;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description *ZM2GB0162:
 * *ZM1GB0161:
 * @Email yinxiaopeng@zj.tech
 * @date 2022-05-03 22:54
 */
public class ZM$GB01$Pattern implements IInstructionParser {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String COMMAND = "*ZM$GB01$$:";

    private UV100Greenboard1000Answer UV100Greenboard1000Answer = new UV100Greenboard1000Answer();
    private UV0Greenboard1000Answer UV0Greenboard1000Answer = new UV0Greenboard1000Answer();

    private DataColorStatus dataColorStatus;

    public ZM$GB01$Pattern(DataColorStatus dataColorStatus) {
        this.dataColorStatus = dataColorStatus;
        init();
    }

    private void init() {
        UV100Greenboard1000Answer.setActions(Arrays.asList(
                new LogInstructionAction(),
                new GreenboardAction()
        ));
        UV0Greenboard1000Answer.setActions(Arrays.asList(
                new LogInstructionAction(),
                new GreenboardAction()
        ));
    }

    @Override
    public Object parse(Object msg, InstructionParserChain chain) {
        String instruction = (String) msg;
        if (!match(instruction)) {
            return chain.filter(msg);
        }

        AnswerResponse response = null;
        if (DataColorStatus.UVEnum.UV100.equals(dataColorStatus.getCurrentUV())) {
            response = UV100Greenboard1000Answer.answer(dataColorStatus, instruction);
        } else {
            response = UV0Greenboard1000Answer.answer(dataColorStatus, instruction);
        }
        return response.getData();
    }


    /**
     * 匹配
     *
     * @param instruction
     * @return
     */
    private boolean match(String instruction) {
        if (null == instruction) {
            return false;
        }

        if (COMMAND.length() != instruction.length()) {
            return false;
        }

        for (int i = 0, len = COMMAND.length(); i < len; i++) {
            String charAt = String.valueOf(COMMAND.charAt(i));
            if ("$".equals(charAt)) {
                continue;
            }
            if (COMMAND.charAt(i) != instruction.charAt(i)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public List<AbstractAnswer> getAnswers() {
        return Arrays.asList(UV100Greenboard1000Answer, UV0Greenboard1000Answer);
    }
}
